package com.zhr.tea.view.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhr.tea.R;

import java.util.List;

;

/**
 * Created by zhang on 2016/11/3.
 */

public class HistoryAdapter extends RecyclerView.Adapter implements View.OnClickListener{


    @Override
    public void onClick(View view) {
        if (recyClickListener!=null){
            recyClickListener.onItemClick((String) view.getTag());
        }
    }

    private RecyClickListener recyClickListener = null;

    public interface RecyClickListener{
        void onItemClick(String tag);
    }


    public void setOnItemClickListener(RecyClickListener r){
        this.recyClickListener=r;
    }





    private Context context;

    private List<String> data;

    public HistoryAdapter(Context context, List<String> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemViewHolder viewHolder = null;
        View view = LayoutInflater.from(context).inflate(R.layout.item_search_history,null);
        viewHolder = new ItemViewHolder(view);
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder && data.size()!=0){
            ((ItemViewHolder) holder).tv.setText(data.get(position));
            holder.itemView.setTag(data.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }




    public class ItemViewHolder extends RecyclerView.ViewHolder {

        TextView tv;
        public ItemViewHolder(View itemView) {
            super(itemView);
            tv = (TextView) itemView.findViewById(R.id.item_history_search);
        }
    }
}
